<?php

class ModelLocalisationCarrier extends Model {
	public function addCarrier($data) {
		$this->db->insert('shipping_carrier', $data);
	}
	
	public function editCarrier($carrier_id, $data) {
		$this->db->update('shipping_carrier', $data, "carrier_id=$carrier_id");
	}
	
	public function getCarrier($carrier_id) {
		return $this->db->queryOne("SELECT * FROM @@shipping_carrier WHERE carrier_id=$carrier_id");
	}
	
	public function getCarriers($qf = null) {
		$sql = "SELECT carrier_id,code,name,tracking_link,description FROM @@shipping_carrier";
		if ($qf) {
			$sql .= $qf->limit();
		}
		return $this->db->queryArray($sql, 'code');
	}
	
	public function getTotalCarriers() {
		return $this->db->queryOne("SELECT COUNT(*) as total FROM @@shipping_carrier");
	}
	
	public function deleteCarrier($carrier_id) {
		if (!is_array($carrier_id)) {
			$carrier_id = array($carrier_id);
		}
		$carrier_ids = implode(',', $carrier_id);
		$this->db->runSql("DELETE FROM @@shipping_carrier WHERE carrier_id IN ($carrier_ids)");
	}
	
	public function getCarrierLabels() {
		$label_configs = $this->db->queryArray("SELECT code, label_config FROM @@shipping_carrier", 'code', 'label_config');
		foreach ($label_configs as $code => $label_config) {
			$label_configs[$code] = v5_html_entity_decode($label_config);
		}
		return $label_configs;
	}
}